package com.jia.leetCode;

/**
 *831. 隐藏个人信息
 *
 */
public class Pro0831 {
    public String maskPII(String S) {
        if(S.contains("@")) {
            return maskEmail(S);
        }
        return maskPhoneNumber(S);
    }

    private String maskPhoneNumber(String s) {
        int countNumber = 0;
        for(int i = 0; i < s.length(); i++) {
            if(s.charAt(i) >= '0' && s.charAt(i) <= '9'){
                ++countNumber;
            }
        }
        s = s.trim();
        StringBuilder sb = new StringBuilder();

        if(countNumber > 10 && (s.charAt(0) == '-' || s.charAt(0) == '+')) {
            sb.append(s.charAt(0));
        }else if(countNumber > 10) {
            sb.append("+");
        }
        for(int i = 10; i < countNumber; i++) {
            sb.append("*");
            if(i == countNumber -1) {
                sb.append("-");
            }
        }
        sb.append("***-***-");

        String str= "";
        for(int i = s.length() -1, count = 0; i>= 0&& count < 4; i--) {
            if(s.charAt(i) >= '0' && s.charAt(i) <= '9'){
                ++count;
                str = s.charAt(i) + str;
            }
        }
        sb.append(str);
        return sb.toString();
    }

    private String maskEmail(String s) {
        s = s.toLowerCase();
        String[] split = s.split("@");
        StringBuilder sb = new StringBuilder();
        sb.append(split[0].charAt(0));
        sb.append("*****");
        sb.append(split[0].charAt(split[0].length()-1));
        sb.append("@");
        sb.append(split[1]);
        return sb.toString();
    }
}
